#include <Arduino.h>
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>
#include <WiFi.h>
#include <Adafruit_AHTX0.h>
#include "BMP.h"
#include <HTTPClient.h>//http连接
#include <ArduinoJson.h>//引入json
#include "font/font_18_weather.h"


#define WIDTH  160
#define HEIGHT 80
#define I2C_SDA 4 //传感器I2C使用的管脚
#define I2C_SCL 5

#define REDLED 12 //WIFI开始连接，亮灯，连接成功后，灯灭
HTTPClient http;

String today_status;
int today_max;
int today_min;
String tomorrow_status;
int tomorrow_max;
int tomorrow_min;
String last_status;
int last_max;
int last_min;
String payload = "";

String UserKey = "key=059629f9abe341af8861e6f99229d979&unit=m&lang=zh&gzip=n";   // 私钥 https://dev.heweather.com/docs/start/get-api-key
String Location = "location=101220109&"; // 城市代码 https://github.com/heweather/LocationList,表中的 Location_ID
// 101190113 雨花台
String base = "https://devapi.qweather.com/v7/weather/3d?";

TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
TFT_eSprite spr = TFT_eSprite(&tft);
const char* ssid     = "Tanggy666";    	 // WIFI账户
//const char* ssid     = "TGYiPhone";
const char* password = "1qaz!QAZ";	 // WIFI密码
//const char* password = "1234567809";	 // WIFI密码


void getW()
{
  String url =base+Location+UserKey;
  //添加请求头
  //http.begin(url); // 准备启用连接
  int httpCode = http.GET(); // 发起GET请求
  String payload;

  if (httpCode > 0) // 如果状态码大于0说明请求过程无异常
  {
    if (httpCode == HTTP_CODE_OK) // 请求被服务器正常响应，等同于httpCode == 200
    {
      payload= http.getString(); // 读取服务器返回的响应正文数据
      //tft.println(payload);
    }else
    {
      payload = "{\"code\":\"200\",\"daily\":[{\"tempMax\":\"32\",\"tempMin\":\"25\",\"textDay\":\"晴\"},{\"tempMax\":\"31\",\"tempMin\":\"24\",\"textDay\":\"晴\"},{\"tempMax\":\"30\",\"tempMin\":\"26\",\"textDay\":\"晴\"}]}";
    }
  }
  else
  {
    Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    payload = "{\"code\":\"200\",\"daily\":[{\"tempMax\":\"32\",\"tempMin\":\"25\",\"textDay\":\"晴\"},{\"tempMax\":\"31\",\"tempMin\":\"24\",\"textDay\":\"晴\"},{\"tempMax\":\"30\",\"tempMin\":\"26\",\"textDay\":\"晴\"}]}";

  }


  http.end(); // 结束当前连接

  const size_t capacity = 2*JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) +
                          JSON_OBJECT_SIZE(5) + 3*JSON_OBJECT_SIZE(26) + 1320;
  DynamicJsonDocument doc(capacity);

  DeserializationError error = deserializeJson(doc,payload); //反序列化JSON数据
  tft.println(error.c_str());
  delay(3000);
  if (!error) //检查反序列化是否成功
    {
      // 得到三天的天气json
      DynamicJsonDocument todayW = doc["daily"][0];
      JsonVariant tomorrowW = doc["daily"][1];
      JsonVariant the_day_after_tomorrowW = doc["daily"][2];
      String today_status_str =todayW["textDay"];
      String tomorrow_status_str =tomorrowW["textDay"];
      String last_status_str =the_day_after_tomorrowW["textDay"];


      today_status = today_status_str;
      today_max = todayW["tempMax"];
      today_min = todayW["tempMin"];

      tomorrow_status = tomorrow_status_str;
      tomorrow_max = tomorrowW["tempMax"];
      tomorrow_min = tomorrowW["tempMin"];

      last_status = last_status_str;
      last_max = the_day_after_tomorrowW["tempMax"];
      last_min = the_day_after_tomorrowW["tempMin"];
    }

}

void show_Weather(){
  tft.fillScreen(TFT_BLACK);//LCD清空
  // String today_status = todayW["textDay"];
  // int today_max = todayW["tempMax"];
  // int today_min = todayW["tempMin"];

  // String tomorrow_status = tomorrowW["textDay"];
  // int tomorrow_max = tomorrowW["tempMax"];
  // int tomorrow_min = tomorrowW["tempMin"];

  // String last_status = the_day_after_tomorrowW["textDay"];
  // int last_max = the_day_after_tomorrowW["tempMax"];
  // int last_min = the_day_after_tomorrowW["tempMin"];
  tft.loadFont(font_18_weather);
  tft.setCursor(0,5);
  tft.setTextColor(TFT_BLUE,TFT_BLACK);
  tft.println("日期  状态 最高 最低");
  tft.setTextColor(TFT_ORANGE,TFT_BLACK);
  tft.setCursor(0,28);
  tft.printf("今天:%6s %d℃ %d℃\n",today_status,today_max,today_min);
  tft.printf("明天:%6s %d℃ %d℃\n",tomorrow_status,tomorrow_max,tomorrow_min);
  tft.printf("后天:%6s %d℃ %d℃\n",last_status,last_max,last_min);
  tft.unloadFont();

}








void setup() {
  Serial.begin(115200);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);


  tft.init();
  // 显示方向
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);//LCD清空
  tft.setCursor(0, 0);
  tft.setTextSize(1);

  WiFi.begin(ssid, password);
  tft.print("WIFI Connecting");

  while (WiFi.status() != WL_CONNECTED)
  {
    //tft.print("WIFI Connecting");
  }
  tft.print("WIFIsuccess");
  getW();


}

void loop() {
  // 延迟时间

  digitalWrite(13,!digitalRead(13));
  digitalWrite(12,!digitalRead(12));
  show_Weather();
  delay(10000);

}
